package com.guanyu.u2cache.redis;

import com.guanyu.u2cache.cache.EventPush;
import com.guanyu.u2cache.constant.RedisConstant;
import com.guanyu.u2cache.enums.CacheType;
import com.guanyu.u2cache.enums.EventType;
import com.guanyu.u2cache.model.EventData;
import com.guanyu.u2cache.util.AddressUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author zhouyunjian
 * @date 2023/11/14 11:47
 * redis事件发布同步
 **/
@Slf4j
//@Component
public class RedisEventPush implements EventPush {
    @Autowired(required = false)
    private RedissonClient redissonClient;

    @Override
    public void publish(String[] eventNames, String key, EventType eventType, CacheType cacheType) {
        log.debug("redis事件通知事件名[{}]key[{}]事件[{}]缓存[{}]", eventNames, key, eventType, cacheType);
        RTopic topic = redissonClient.getTopic(RedisConstant.topic);
        EventData eventData = new EventData();
        eventData.setCacheNames(eventNames);
        eventData.setKey(key);
        eventData.setMacAddress(AddressUtil.getMacAddress());
        eventData.setEventType(eventType);
        eventData.setCacheType(cacheType);
        topic.publish(eventData);
    }
}
